/**
 * 
 */
package com.hera.process.web.primefaces.controller;

import javax.faces.application.FacesMessage;
import javax.inject.Inject;
import javax.servlet.ServletContext;

import org.primefaces.push.EventBus;
import org.primefaces.push.RemoteEndpoint;
import org.primefaces.push.annotation.OnClose;
import org.primefaces.push.annotation.OnMessage;
import org.primefaces.push.annotation.OnOpen;
import org.primefaces.push.annotation.PathParam;
import org.primefaces.push.annotation.PushEndpoint;
import org.primefaces.push.annotation.Singleton;
import org.primefaces.push.impl.JSONEncoder;

/**
 * @author cmontes
 *
 */
@PushEndpoint("/inbox/{user}")
@Singleton
public class NotifyResource {
	
	private String room="/inbox";
	/*@PathParam("user")
	private String username;*/
	@Inject
	private ServletContext ctx;
	
	@OnOpen
	public void onOpen(RemoteEndpoint r, EventBus eventBus){		
		eventBus.publish(this.room + "/*", "onOpen");
	}
	  
	@OnClose
	public void onClose(RemoteEndpoint r, EventBus eventBus){
		//ChatUsers users = (ChatUsers)this.ctx.getAttribute("chatUsers");
		//users.remove(this.username);
		eventBus.publish(this.room + "/*", "onClose");
	}
	  
	@OnMessage(encoders = {JSONEncoder.class})
    public FacesMessage onMessage(FacesMessage message) {
		System.out.println(message);
        return message;
    }

}
